package com.ktjiaoyu.crm.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktjiaoyu.crm.mapper.RoleMapper;
import com.ktjiaoyu.crm.pojo.Role;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName : RoleServiceImpl
 * @Description :
 * @Author : Lzz
 * @Date: 2023/11/22 10:33:35
 */
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    RoleMapper roleMapper;

    /*@Resource
    RedisTemplateUtil redisTemplateUtil;*/

    @Cacheable(cacheNames = "role",key = "'roleList'",unless = "#result == null")
    @Override
    public List<Role> findAllRoleList() {
        /*String key = "role："+"roleList";
        //首次进入先查询redis中是否有缓存数据
        if(redisTemplateUtil.hasKey(key)){
            List<Role> roleList = (List<Role>) redisTemplateUtil.get(key);
            return roleList;
        }
        //如果redis中没有缓存数据，则先查询数据库，然后将查询到的数据存入redis
        List<Role> roleList = roleMapper.selectList(null);
        if(roleList.size()>0){
            redisTemplateUtil.set(key, roleList);
        }*/
        return roleMapper.selectList(null);
    }

    @CachePut(cacheNames = "role",key = "'roleList'")
    @Transactional
    @Override
    public int addRole(Role role) {
        /*int result = roleMapper.insert(role);
        String key = "role："+"roleList";
        if (result >= 1){
            redisTemplateUtil.del(key);
        }*/
        return roleMapper.insert(role);
    }
}
